﻿using Business.Base.Model;
using Business.ScrewPump.Perf;
using Component;
using Pumptester.ExperimentForms.Base;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pumptester.ExperimentForms.ScrewPump.Perf
{
    class ScrewPumpPerfChart : IChartDrawer
    {
        public void DrawChart(BaseChartData chartData, UserChart chart)
        {
            ScrewPumpPerfChartData chartDatainfo = (ScrewPumpPerfChartData)chartData;

            #region 初始化坐标系
            Component.Axis xAxis = new Component.Axis();
            xAxis.Name = "压力";
            xAxis.MaxValue = chartDatainfo.MaxXPressAxis;
            xAxis.MinValue = chartDatainfo.MinXPressAxis;
            xAxis.SegCount = chartDatainfo.XUnitCount;

            Component.Axis yAxis = new Component.Axis();
            yAxis.Name = "流量";
            yAxis.MaxValue = chartDatainfo.MaxYQAxis;
            yAxis.MinValue = chartDatainfo.MinYQAxis;
            yAxis.SegCount = chartDatainfo.YUnitCount;


            Component.Axis ySencodAxis = new Component.Axis();
            ySencodAxis.Name = "功率";
            ySencodAxis.MaxValue = chartDatainfo.MaxYPAxis;
            ySencodAxis.MinValue = chartDatainfo.MinYPAxis;

            Component.Axis yThirdAxis = new Component.Axis();
            yThirdAxis.Name = "效率";
            yThirdAxis.MaxValue = chartDatainfo.MaxYEAxis;
            yThirdAxis.MinValue = chartDatainfo.MinYEAxis;
            #endregion

            #region 注释
            /*
            Coordinate coordinate = new Coordinate();
            coordinate.XAxis = xAxis;
            coordinate.YAxis = yAxis;
            coordinate.AxisDescritionBrush = new SolidBrush(Color.White);
            coordinate.AxisPen = new Pen(Color.White);
            coordinate.CurvePen = new Pen(new SolidBrush(Color.Blue), 2);
            coordinate.PointBrush = new SolidBrush(Color.Yellow);

            Coordinate secondCoordinate = new Coordinate();
            secondCoordinate.XAxis = xAxis;
            secondCoordinate.YAxis = ySencodAxis;
            secondCoordinate.CurvePen = new Pen(new SolidBrush(Color.Gray), 2);
            secondCoordinate.PointBrush = new SolidBrush(Color.Orange);

            Coordinate thirdCoordinate = new Coordinate();
            thirdCoordinate.XAxis = xAxis;
            thirdCoordinate.YAxis = yThirdAxis;
            thirdCoordinate.CurvePen = new Pen(new SolidBrush(Color.Silver), 2);
            thirdCoordinate.PointBrush = new SolidBrush(Color.Red);

            SolidBrush defaultAuxiliaryAxisPointBrush = new SolidBrush(Color.White);
            coordinate.AuxiliaryAxisPointBrush = secondCoordinate.AuxiliaryAxisPointBrush = thirdCoordinate.AuxiliaryAxisPointBrush = defaultAuxiliaryAxisPointBrush;
            thirdCoordinate.AxisDescritionBrush = secondCoordinate.AxisDescritionBrush = coordinate.AxisDescritionBrush = new SolidBrush(Color.White);
            thirdCoordinate.AxisPen = secondCoordinate.AxisPen = coordinate.AxisPen = new Pen(Color.White);
            */
            #endregion

            #region 初始化各种曲线和图形的画笔颜色，粗细等属性
            Coordinate coordinate = new Coordinate();
            coordinate.XAxis = xAxis;
            coordinate.YAxis = yAxis;
            coordinate.AxisDescritionBrush = new SolidBrush(Color.White);
            coordinate.AuxiliaryAxisPointBrush = new SolidBrush(Color.White);
            coordinate.AxisPen = coordinate.AxisPen = new Pen(Color.White);
            coordinate.CurvePen = new Pen(new SolidBrush(Color.White), 1);
            coordinate.PointBrush = new SolidBrush(Color.Yellow);
            coordinate.LinePen = new Pen(new SolidBrush(Color.White), 1);
            coordinate.AuxiliaryBrush = coordinate.AxisDescritionBrush = new SolidBrush(Color.White);


            Coordinate secondCoordinate = new Coordinate();
            secondCoordinate.XAxis = xAxis;
            secondCoordinate.YAxis = ySencodAxis;
            secondCoordinate.CurvePen = secondCoordinate.AxisPen = new Pen(new SolidBrush(Color.Silver), 1);
            secondCoordinate.PointBrush = secondCoordinate.AuxiliaryAxisPointBrush = new SolidBrush(Color.Orange);
            secondCoordinate.AuxiliaryBrush = secondCoordinate.AxisDescritionBrush = new SolidBrush(Color.Silver);

            Coordinate thirdCoordinate = new Coordinate();
            thirdCoordinate.XAxis = xAxis;
            thirdCoordinate.YAxis = yThirdAxis;
            thirdCoordinate.CurvePen = thirdCoordinate.AxisPen = new Pen(new SolidBrush(Color.Gray), 1);
            thirdCoordinate.PointBrush = thirdCoordinate.AuxiliaryAxisPointBrush = new SolidBrush(Color.Red);
            thirdCoordinate.AuxiliaryBrush = thirdCoordinate.AxisDescritionBrush = new SolidBrush(Color.Gray);
            #endregion

            #region 画每个坐标系中采集点的信息

            for (int i = 0; i < chartDatainfo.InputPoints.Count; i++)
            {               
                double press = chartDatainfo.InputPoints[i][0];
                double q = chartDatainfo.InputPoints[i][1];
                double p = chartDatainfo.InputPoints[i][2];
                double e = chartDatainfo.InputPoints[i][3];
                coordinate.Points.Add(new Component.Point(press, q));
                secondCoordinate.Points.Add(new Component.Point(press, p));
                thirdCoordinate.Points.Add(new Component.Point(press, e));
            }

            #endregion

            #region 画每个坐标系中拟合的曲线

            int pointCount = chartDatainfo.DrawList.Count;
            List<Component.Point> firstPoints = new List<Component.Point>();
            List<Component.Point> secondPoints = new List<Component.Point>();
            List<Component.Point> thirdPoints = new List<Component.Point>();

            for (int i = 0; i < pointCount; i++)
            {
                double[] data = chartDatainfo.DrawList[i];
                double x = data[0];
                double[] qePoints = new double[] { x, data[3] };
                double[] qhPoints = new double[] { x, data[1] };
                double[] qpPoints = new double[] { x, data[2] };

                Component.Point p1 = new Component.Point(qhPoints[0], qhPoints[1]);
                firstPoints.Add(p1);

                Component.Point p2 = new Component.Point(qpPoints[0], qpPoints[1]);
                secondPoints.Add(p2);

                Component.Point p3 = new Component.Point(qePoints[0], qePoints[1]);
                thirdPoints.Add(p3);
            }
            Curve firstCurve = new Curve(firstPoints);
            Curve secondCurve = new Curve(secondPoints);
            Curve thirdCurve = new Curve(thirdPoints);
            coordinate.Curves.Add(firstCurve);
            secondCoordinate.Curves.Add(secondCurve);
            thirdCoordinate.Curves.Add(thirdCurve);
            #endregion

            #region 画出实测值下压力的允差范围

            //如果实测值不为负数，展示允差范围
            if (chartDatainfo.MeasurePresure - 0 > double.Epsilon)
            {
                //画允差范围
            if (chartDatainfo.ShowFlowRange && chartDatainfo.FlowRange != null && chartDatainfo.FlowRange.Length >= 1)
            {
                Component.Point pointStart = new Component.Point(chartDatainfo.MeasurePresure,chartDatainfo.FlowRange[0]);
                Component.Point pointEnd   = new Component.Point(chartDatainfo.MeasurePresure, chartDatainfo.FlowRange[1]);
                coordinate.Lines.Add(new Line(pointStart,pointEnd, Color.Yellow));
            }
            if (chartDatainfo.ShowPowerRange && chartDatainfo.PowerRange != null && chartDatainfo.PowerRange.Length >= 1)
            {
                Component.Point pointStart = new Component.Point(chartDatainfo.MeasurePresure, chartDatainfo.PowerRange[0]);
                Component.Point pointEnd = new Component.Point(chartDatainfo.MeasurePresure, chartDatainfo.PowerRange[1]);
                secondCoordinate.Lines.Add(new Line(pointStart, pointEnd, Color.Orange));
            }
            if (chartDatainfo.ShowEffRange && chartDatainfo.EffRange != null && chartDatainfo.EffRange.Length >= 1)
            {
                Component.Point pointStart = new Component.Point(chartDatainfo.MeasurePresure, chartDatainfo.EffRange[0]);
                Component.Point pointEnd = new Component.Point(chartDatainfo.MeasurePresure, chartDatainfo.EffRange[1]);
                thirdCoordinate.Lines.Add(new Line(pointStart, pointEnd, Color.Red));
            }
            }

            #endregion

            #region 画出额定压力和额定流量的曲线
            //画额定压力和额定流量的直线
            coordinate.Lines.Add(new Line(new Component.Point(0, 0), new Component.Point(chartDatainfo.XMaxFlowinRatePresureLine, chartDatainfo.YMaxFlowinRatePresureLine)));
            #endregion

            /*if (!Double.IsNaN(chartDatainfo.MeasurePresure))
            {
                double measureFlow = chartDatainfo.MeasureFlow;
                double measurePresure = chartDatainfo.MeasurePresure;
                coordinate.Lines.Add(new Line(new Component.Point(measurePresure, 0), new Component.Point(measurePresure, measureFlow)));
            }*/

            chart.FirstCoordinate = coordinate;
            chart.SecondCoordinate = secondCoordinate;
            chart.ThirdCoordinate = thirdCoordinate;

            chart.Refresh();
        }
    }
}
